const userInfoMap = new Map();

class FriendInfo {
    friendName;
    messageHistory;

    constructor(friendName, messageHistory = []) {
        this.friendName = friendName;
        this.messageHistory = messageHistory;
    }
}
// 充当结构体,发送的信息

class Message {
    sender;
    receiver;
    content;
    date;

    constructor(sender, receiver, content, date) {
        this.sender = sender;
        this.receiver = receiver;
        this.content = content;
        this.date = date;
    }
}
// 充当结构体，用户
class UserInfo {
    userName;
    password;
    isOnline;
    friendInfoMap;
    age;
    sex;

    constructor(userName, password, isOnline = false, friendInfoMap = new Map(), age = 24, sex = 0) {
        this.userName = userName;
        this.password = password;
        this.isOnline = isOnline;
        this.friendInfoMap = friendInfoMap;
        this.age = age;
        this.sex = sex;
    }
}

// 添加朋友
function addFriend(uA, uB) {

    if (!userInfoMap.has(uA) || !userInfoMap.has(uB)) {
        console.log(`add friend ${uA} + ${uB} failed,no such user`);
        return false;
    }
    if (userInfoMap.get(uA).friendInfoMap.has(uB) || userInfoMap.get(uB).friendInfoMap.has(uA)) {
        console.log(`add friend ${uA} + ${uB} failed,they are already friends`);
        return false;
    }

    userInfoMap.get(uA).friendInfoMap.set(uB, new FriendInfo(friendName = uB));
    userInfoMap.get(uB).friendInfoMap.set(uA, new FriendInfo(friendName = uA));
    console.log(`add friend ${uA} + ${uB}`);
    return true;
}


// 添加用户
function addUserInfo(username, userinfo) {
    if (userInfoMap.has(username)) {
        console.log('already exist');
        return false;
    }
    userInfoMap.set(username, userinfo);
    return true;
}

// 删除用户
function deleteUserInfo(username) {
    if (!userInfoMap.has(username)) {
        console.log('no such user');
        return false;
    }
    userInfoMap.delete(username);
    return true;
}

// 获取用户信息
function getUserInfo(username) {
    if (!userInfoMap.has(username)) {
        console.log('no such user');
        return NaN;
    }
    return userInfoMap.get(username);
}

// 检查用户名密码
function checkPassword(username, password) {
    if (!userInfoMap.has(username)) {
        console.log('no such user');
        return false;
    }
    if (userInfoMap.get(username).password === password) {
        return true;
    }
    return false;
}

// 增加信息
function addMessage(message) {
    userInfoMap.get(message.sender).friendInfoMap.get(message.receiver).messageHistory.push(message);
    userInfoMap.get(message.receiver).friendInfoMap.get(message.sender).messageHistory.push(message);
}
// 设置在线状态
function setOnlineState(username, isOnline) {
    userInfoMap.get(username).isOnline = isOnline;
    console.log(`${username} set OnlineState ${isOnline}`);
}
// 获取在线状态
function getOnlineState(username) {
    let isOnlineState = userInfoMap.get(username).isOnline;
    console.log(`${username} get OnlineState ${isOnlineState}`);
    return isOnlineState;

}

// 获取好友列表
function getFriendList(username) {
    return Array.from(userInfoMap.get(username).friendInfoMap.keys());
}
// 获取聊天内容
function getMessageHistory(userA, userB) {
    return userInfoMap.get(userA).friendInfoMap.get(userB).messageHistory;
}
module.exports = {
    UserInfo,
    FriendInfo,
    Message,
    addFriend,
    addUserInfo,
    deleteUserInfo,
    getUserInfo,
    checkPassword,
    addMessage,
    setOnlineState,
    getOnlineState,
    getFriendList,
    getMessageHistory
};